package cn.edu.nuc.creditSystem.controller.user;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;

import cn.edu.nuc.creditSystem.bean.Msg;
import cn.edu.nuc.creditSystem.bean.user.User;
import cn.edu.nuc.creditSystem.controller.BaseController;
import cn.edu.nuc.creditSystem.service.BaseService;
import cn.edu.nuc.creditSystem.service.user.service.UserService;
@Controller
@RequestMapping("/cs/user")
public class UserController extends BaseController<User>{
	@Autowired
	private UserService us;
	@Override
	protected BaseService<User> getService() {
		return us;
	}

	@Override
	protected String getPrefix() {
		return "/user/";
	}
	@RequestMapping("/resetPass")
	@ResponseBody
	private Msg resetPassword(User user){
		if(user.getId()!=null){
			us.ForgetPass(us.get(user.getId()));
			return new Msg(Msg.success,"您的密码已重置成功！");
		}else{
			return new Msg(Msg.error,"密码重置失败！");
		}
	}
	@RequestMapping(value = "/saveUser", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String save(User model) throws Exception {
		Gson g=new Gson();
		if (model.getId() == null) {
			return g.toJson(us.insertUser(model));
		} else {
			return g.toJson(us.updateUser(model));
		}
	}
	@RequestMapping("/updatePass")
	@ResponseBody
	public String updatepass(User user){
		Gson g=new Gson();
		if(user.getPassword()==null && "".equals(user.getPassword()))return g.toJson(-1);
		return g.toJson(us.update(user));
	}
	@RequestMapping("/logout")
	public String logout(HttpSession session){
		session.removeAttribute("user");
		return "redirect:/";
	}
}
